<template>
  <div class="consult-item">
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/images/avatar-doctor.svg" alt="" />
      <p>{{ consultOrder.docInfo?.name }}</p>
      <span
        :class="{
          orange: consultOrder.status === OrderType.ConsultPay,
          green: consultOrder.status === OrderType.ConsultChat
        }"
        >{{ consultOrder.statusValue }}</span
      >
    </div>
    <div class="body" @click="$router.push(`/my/consult/${consultOrder.id}`)">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">{{ consultOrder.illnessDesc }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">￥{{ consultOrder.payment }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ consultOrder.createTime }}</div>
      </div>
    </div>
    <div class="foot" v-if="consultOrder.status === OrderType.ConsultPay">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loading"
        @click="cancelOrder(consultOrder)"
      >
        取消问诊
      </van-button>
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/my/consult/${consultOrder.id}`"
      >
        去支付
      </van-button>
    </div>
    <div class="foot" v-if="consultOrder.status === OrderType.ConsultWait">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loading"
        @click="cancelOrder(consultOrder)"
      >
        取消问诊
      </van-button>
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${consultOrder.id}`"
      >
        继续沟通
      </van-button>
    </div>
    <div class="foot" v-if="consultOrder.status === OrderType.ConsultChat">
      <van-button
        v-if="consultOrder.prescriptionId"
        class="gray"
        plain
        size="small"
        round
        @click="showPrescription(consultOrder.prescriptionId)"
      >
        查看处方
      </van-button>
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${consultOrder.id}`"
      >
        继续沟通
      </van-button>
    </div>
    <div class="foot" v-if="consultOrder.status === OrderType.ConsultComplete">
      <div class="more">
        <van-popover
          placement="top-start"
          v-model:show="showPopover"
          :actions="actions"
          @select="onSelect"
        >
          <template #reference> 更多 </template>
        </van-popover>
      </div>
      <van-button
        class="gray"
        plain
        size="small"
        round
        :to="`/room?orderId=${consultOrder.id}`"
      >
        问诊记录
      </van-button>
      <van-button
        v-if="!consultOrder.evaluateId"
        type="primary"
        plain
        size="small"
        round
      >
        去评价
      </van-button>
      <van-button v-else class="gray" plain size="small" round>
        查看评价
      </van-button>
    </div>
    <div class="foot" v-if="consultOrder.status === OrderType.ConsultCancel">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="deleteLoading"
        @click.stop="deleteOrder(consultOrder.id)"
      >
        删除订单
      </van-button>
      <van-button type="primary" plain size="small" round to="/"
        >咨询其他医生</van-button
      >
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, inject } from 'vue'
import { PropType } from 'vue'
import { OrderType } from '@/enums'
import { ConsultOrderItem } from '@/types/consult'
import { useDeleteOrder, useShowPrescription, useCancelOrder } from '@/hook'
import { ImagePreview } from 'vant'
import { ApiRes } from '@/types/data'
import request from '@/utils/request'
const props = defineProps({
  consultOrder: {
    type: Object as PropType<ConsultOrderItem>,
    required: true
  }
})
// 取消订单
const { loading, cancelOrder } = useCancelOrder()
// 删除订单
const deleteOrderItem = inject('deleteOrderItem') as (id: string) => void
const { loading: deleteLoading, deleteOrder } = useDeleteOrder(() => {
  deleteOrderItem(props.consultOrder.id)
})
const showPopover = ref(false)
const actions = computed(() => [
  { text: '查看处方', disabled: !props.consultOrder.prescriptionId },
  { text: '删除订单' }
])
const { showPrescription } = useShowPrescription()
const onSelect = async (action: { text: string }, i: number) => {
  console.log(action.text, i)
  if (i === 0) {
    // 查看处方
    showPrescription(props.consultOrder.prescriptionId)
  }
  if (i === 1) {
    // 删除
    deleteOrder(props.consultOrder.id)
    // 刷新
    deleteOrderItem(props.consultOrder.id)
  }
}
</script>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      &.tip {
        color: var(--cp-tip);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
    .more {
      color: var(--cp-tag);
      flex: 1;
      font-size: 13px;
    }
  }
}
</style>
